<extend name="Public/base"/>

<block name="style">
    <link rel="stylesheet" type="text/css" href="__ADDONROOT__/View/css/vote.css">
</block>
<block name="body">
    <div class="main-title cf">
        <h2>{$meta_title}</h2>
    </div>
    <!-- 标签页导航 -->
    <div class="tab-wrap">
        <div class="tab-content">
            <!-- 表单 -->
            <form class="form-horizontal">

                <div class="form-item">
                    <label class="item-label">
                        标题：
                        <span class="check-tips">
                        必填项
                        </span>
                    </label>

                    <div class="controls">
                        <input class="text input-large" value="{$data['title']}" type="text" id="title" name="title"/>
                    </div>
                </div>

                <div class="form-item">
                    <label class="item-label">
                        说明：
                    </label>

                    <div class="textarea input-large">
                        <textarea id="description" rows="3">{$data['description']}</textarea>
                    </div>
                </div>

                <div class="form-item">
                    <label class="item-label">
                        选项：
                        <span class="check-tips">
                            最多可填写 30 个选项
                        </span>
                        <span class="pull-right">
                        <input type="checkbox" name="voteconfig" value="2" id="voteconfig" <eq name="data['voteconfig']" value="2" >checked="checked"</eq> >多选模式</span>
                    </label>

                    <div class="vote controls">

                 <php>
                if($data){
                $options = $data["options"];
                $options = json_decode($options, true);
                  </php>

                        <volist name="options" id="op">
                            <li class="votechoice">
                                <input class="text input-large" value=" {$op['value']}" type="text" name="choice"/>
                                <span class="votedel" title="删除"></span>
                            </li>
                        </volist>

                       <php>
                }else{

               </php>

                        <li class="votechoice">
                            <input class="text input-large" type="text" name="choice"/>
                            <span class="votedel" title="删除"></span>
                        </li>
                        <li class="votechoice">
                            <input class="text input-large" type="text" name="choice"/>
                            <span class="votedel" title="删除"></span>
                        </li>
                        <li class="votechoice">
                            <input class="text input-large" type="text" name="choice"/>
                            <span class="votedel" title="删除"></span>
                        </li>
                      
               <php>
                }
                </php>
                        <li class="addchoice">
                            <span class="btn btn-default pull-right" id="addchoice" title="增加一个选项">+增加一个</span>
                        </li>
                    </div>
                </div>

                <div class="form-item">
                    <label class="item-label">
                        备注：
                    </label>

                    <div class="textarea input-large">
                        <textarea id="explanation" rows="3">{$data['explanation']}</textarea>
                    </div>
                </div>

                <div class="form-item">
                    <div class="controls">
                         <span class="btn submit-btn" id="complete">确定</span>
                         <button onclick="javascript:history.back(-1);return false;" class="btn btn-return">返回</button> 
                    </div>
                </div>
                <input type="hidden" id="options">
                <input type="hidden" id="id" name="id" value="{$data['id']}">
            </form>


        </div>
    </div>
    
</block>

<block name="script">

    <script type="text/javascript">
	
        var vote_max =30;
		
		var vote_min=2;
		
        var vote_count=3;

        $(document).on("click", ".votedel", function () {
			
			if(vote_count<=vote_min){
				
				alert("不能少于"+vote_min+"个选项!");
				
				return false;
			}	
			
            vote_count--;
			
            $(this).parent().remove();
			
        });


        (function () {
            $("#maxnum").val(vote_max);

            $("#addchoice").click(function () {
                if (vote_count < vote_max) {

                    vote_count++;
                    $('<li class="votechoice">' +
                            '<input class="text input-large" type="text" name="choice"/><span class="votedel" title="删除"></span></li>')
                            .insertBefore(".addchoice");
                }
                else {
                    alert("已经" + vote_max + "个了，不能在添加了!");
                }
            });
            $("#complete").click(function () {
                var nullcheck = true;
                var json = '[';
                $.each($(".votechoice"), function (i, item) {
                    if ($(item).find("input").val() == "") {
                        alert("选项内容不能有空的!");
                        nullcheck = false;
                        return nullcheck;
                    }
                    if (i > 0) {
                        json += ',';
                    }
                    json = json + '{"id":"' + i + '","value":"' + $(item).find("input").val() + '","num":"0","percent":"0"}';

                });
                if (!nullcheck) {
                    return;
                }
                json += ']';

                $("#options").val(json);
                var save_post_action = "{:addons_url('Vote://VoteAdmin/saveVote')}";
                var id = $.trim($("#id").val());
                var title = $.trim($("#title").val());
                var description = $.trim($("#description").val());
                var options = $.trim($("#options").val());
                var explanation = $.trim($("#explanation").val());
                var op = $("input[name='voteconfig']:checked").val();
                var voteconfig = 1;
                if (op) {
                    voteconfig = op;
                }
                $.post(save_post_action, {id:id,title: title, description: description, options: options, explanation: explanation, voteconfig: voteconfig}, function (data) {
                    if (data) {
						
						updateAlert('操作成功,页面即将自动跳转~','alert-success');
						
						setTimeout(function(){
											
                        location.href = "{:U('adminlist',array('name'=>'Vote'))}";
						
						},1500);
						
                    } else {
						
                        updateAlert('提交数据出错!','alert-error');
						
						setTimeout(function(){
											
						 location.reload();
						 
						},1500);
						
                    }

                }, "text");
            });


        })();
    </script>
</block>
